import Vue from "vue";
import Vuex from "vuex";
import Storage from "@/uitl/Storagehandle"; //sessionStorage的操作

import InternshipApply from "./InternshipApply"; //申请实习的组件store
import IntemshipGroup from "./IntemshipGroup"; //实习小组的组件store
Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    //因为vuex的数据在页面刷新时会重置（映射到Storagehandle）
    token: Storage.getItem("token") || "",
    UserInfo: Storage.getItem("UserInfo") || {},
    //home的侧边栏选中项
    HomeSelectedKeys: Storage.getItem("HomeSelectedKeys") || ["0"], //存储在session为空时默认["0"]
    //home选中的实习小组的key
    selectGroupKey: Storage.getItem("selectGroupKey") || "",
    selectGroupName: Storage.getItem("selectGroupName") || "",
    //home侧边栏的锁定状态
    homeSelectedState: false,
  },
  getters: {},
  mutations: {
    //存储登录后的token以及用户信息
    saveUserInfo(state, data) {
      Storage.setItem(`UserInfo`, data.user);
      state.UserInfo = data.user;
      Storage.setItem(`token`, data.token);
      state.token = data.token;
    },
    //删除信息
    removeUserInfo(state) {
      state.UserInfo = {};
      state.token = "";
      state.HomeSelectedKeys = ["0"];
      Storage.clear();
    },
    setHomeSelectedKeys(state, keys) {
      Storage.setItem(`HomeSelectedKeys`, keys);
      state.HomeSelectedKeys = keys;
    },
    //设置选择的key
    setGroupKey(state, data) {
      Storage.setItem(`selectGroupKey`, data.key);
      state.selectGroupKey = data.key;
      Storage.setItem(`selectGroupName`, data.name);
      state.selectGroupName = data.name;
    },
    setHomeSelectedState(state){
      state.homeSelectedState=!state.homeSelectedState;
    }
  },
  actions: {},
  modules: {
    InternshipApply,
    IntemshipGroup,
  },
});
